//
// Copyright (c) 深讯信息科技  2015, 保留所有权利。
//
// CheckMappingNoCst.java
//
// 作    者：shiming
// 当前版本：1.0
// 2015-3-23 下午04:00:42
//
//
package com.vanke.agent.common;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import com.vanke.agent.ADataSource;

/**
 * 扫描所有映射表，检查没有主档的映射
 * @author ysm
 * @date 2015-3-23 下午04:00:55
 */
public class CheckMappingNoCst extends Cache {
	public static void main(String[] args)  {
		System.out.println("=========CheckMappingNoCst start==========");
		Connection bizCon = ADataSource.getConnection(ADataSource.SCHEMA_BIZ);
		
		
		//加载所有的映射：手机、身份证、明源GUID
		int count=0;
		
		//检查数据中心主档是否都有映射
		int sum=0;
		String[] types=new String[]{
				MOBILE,
				CARDID,
				ORIGUID};
		Set<Long> cstSet=loadCst(bizCon);
		for(String type:types){

			if(type.equals(MOBILE)){
				count=Cache.loadMobileMapping(bizCon);
			}else if(type.equals(CARDID)){
				count=Cache.loadCardIdMapping(bizCon);
			}else if(type.equals(ORIGUID)){
				count=Cache.loadGUIDMapping(bizCon);
			}
			System.out.println(type+" mapping count:"+count);

			for(String table:cacheMap.get(type).keySet()){
			int size=cacheMap.get(type).get(table).size();
			sum+=size;
			for(String mapping:cacheMap.get(type).get(table).keySet()){
				long cstGUID=cacheMap.get(type).get(table).get(mapping);
				if (!cstSet.contains(cstGUID)){
					System.out.println(type+","+mapping+":"+cstGUID+" lack Customer info!!!");
				}
			}
			//System.out.println("table:"+table+",rowsize:"+size);
		}
		System.out.println(type+" sum:"+sum);
/*		147274364
		146933081
		147149470
		146907753
		147322730*/
		long[] cstGUIDs={147274364,146933081,147149470,146907753,147322730,148166721};
		for(long id:cstGUIDs){
			Set<String> m1=getMobiles(id);
			System.out.println("mobile mappings:"+Arrays.toString(m1.toArray()));
			
			Set<String> m2=getCardIds(id);
			System.out.println("cardid mappings:"+Arrays.toString(m2.toArray()));
			
			Set<String> m3=getOriGuids(id);
			System.out.println("guid mappings:"+Arrays.toString(m3.toArray()));
			System.out.println("-------");
		}
		Cache.releaseCache();
		try {
			Thread.sleep(5000);
		} catch (InterruptedException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		}
		}
		
		ADataSource.close(bizCon);
		System.out.println("=========CheckMappingNoCst end==========");
	}
	private static Set<Long> loadCst(Connection bizCon){
//		加载数据中心所有客户ID列表
		Set<Long> cstSet=new HashSet<Long>();
		List<String> cstTables=Cache.loadTables(bizCon, "cst_customer_");
		System.out.println(cstTables);
		for(String s:cstTables){
			Set<Long> set;
			try {
				set = getCstSet(s,bizCon);
				System.out.println("customer size:"+set.size());
				cstSet.addAll(set);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		System.out.println("all customer size:"+cstSet.size());
		return cstSet;
	}
	public static final Set<Long> getCstSet(String cstTable,Connection conn) throws SQLException{
		Set<Long> cstSet=new HashSet<Long>();
		PreparedStatement ps=conn.prepareStatement("select cstGUID from "+cstTable);
		ResultSet rs=ps.executeQuery();
		while(rs.next()){
			cstSet.add(rs.getLong(1));
		}
		return cstSet;
	}
	
}
